package com.motong.gongdan.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motong.gongdan.config.ResponseData;
import com.motong.gongdan.config.auth.UserLoginToken;
import com.motong.gongdan.entity.LampBox;
import com.motong.gongdan.service.LampBoxService;
import com.motong.gongdan.util.LoginUtil;
import com.motong.gongdan.vo.system.common.query.IdValueQuery;
import com.motong.gongdan.vo.system.lampBox.query.LampBoxListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Api(tags = "联络灯盒子配置")
@RestController
@RequestMapping("/sys/lampBox")
public class LampBoxController {

    @Resource
    private LampBoxService lampBoxService;

    @PostMapping("/getList")
    @ApiOperation("获取联络灯盒子信息")
    @UserLoginToken
    public ResponseData<List<LampBox>> getList(@RequestBody(required = false) LampBoxListQuery query){
        query = Optional.ofNullable(query).orElse(new LampBoxListQuery());
        Page<LampBox> data =  lampBoxService.getList(new Page<>(query.getPage(),query.getLimit()),query);
        return ResponseData.success(data.getRecords(),data.getTotal());
    }

    @PostMapping("/edit")
    @ApiOperation("编辑联络灯盒子配置")
    @UserLoginToken
    public ResponseData<String> edit(@RequestBody LampBox lampBox){
        lampBox.setUpdateTime(new Date()).setUpdateUser(LoginUtil.getLoginUserId());
        lampBoxService.saveOrUpdate(lampBox);
        return ResponseData.success("编辑成功");
    }

    @ApiOperation("删除联络灯盒子配置")
    @PostMapping("/del")
    @UserLoginToken
    @Transactional
    public ResponseData<String> del(@RequestBody IdValueQuery query) {
        if (!CollectionUtils.isEmpty(query.getIdList())){
            lampBoxService.removeByIds(query.getIdList());
        }
        return ResponseData.success("删除成功!");
    }



}
